<template>
    <div id="authorityGroupManage" class="center_div">
        <div class="searchForm" style="margin-top: 1%;margin-left: 1%;">
            <el-form :inline="true"  class="demo-form-inline">
                <el-form-item label="权限组">
                    <el-input v-model="name"  ></el-input>

                </el-form-item>
                <el-form-item>
                    <el-button type="primary"  @click="querySearch" class="dataManageBtn22BtnpPx">查询
                    </el-button>
                    <el-button type="primary"  @click="add" class="dataManageBtn22BtnpPx">添加</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!--  <div class="button" style="margin-top: 1%;margin-left: 1%">
              <el-button type="primary"  size="mini" @click="add">添加</el-button>
          </div>-->
        <div class="table-wrap">
            <el-table
                    :data="tableData"
                    class="tableHandle"
                    border
                    header-align="center"
                    cell-class-name="dataManage18px"
                    :header-cell-style="headerCellClassName">
                <el-table-column align="center" v-for="title in tableTitle" :prop="title.prop" :label="title.label"
                                 :show-overflow-tooltip="title.overflowTooltip">
                </el-table-column>
                <template v-if="buttons && buttons.length!==0">
                    <el-table-column
                            class="align-right"
                            width="250px"
                            header-align="center"
                            label="操作"
                            align="center">
                        <template slot-scope="scope">
                            <el-button v-for="button in buttons"
                                       @click="operate(scope.row,button.href)" :type="button.type"
                                       class="el-button el-button--text dataManageBtn22px">{{button.label}}
                            </el-button>
                        </template>
                    </el-table-column>
                </template>
            </el-table>
            <el-pagination
                    class="dataManage18px"
                    layout="total,prev, pager, next"
                    background
                    @current-change="changePageNum"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    :total="total">
            </el-pagination>
        </div>
        <div>
            <el-dialog :title="title"
                       @close="closeDialog"

                       :visible.sync="showBox" width="65%" append-to-body>
                <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="权限组" prop="name">
                                <el-input v-model="form.name" clearable></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="描述" prop="remark">
                                <el-input v-model="form.remark" clearable></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!------------------------调度------------------------->
                    <br>
                    <div style="border:1px solid #EBEBEB ;padding: 10px">
                        <el-row style="border: 1px silver">
                            <!--                            <el-col :span="6" style="visibility: hidden" id="checkVoiceAllExist">-->
                            <el-col :span="4" :style="menu.csscheckedDispatchVoiceOptionsAll">
                                <el-checkbox :indeterminate="isIndeterminateDispatchVoice"
                                             v-model="menu.checkedDispatchVoiceOptionsAll"
                                             @change="handlecheckedDispatchVoiceOptionsAllAllChange"
                                >调度
                                </el-checkbox>
                            </el-col>

                            <el-col :span="20">
                                <div style="border:1px solid #EBEBEB;border-top:none;border-right:none;border-bottom:none; ">
                                    <el-checkbox-group v-model="checkedDispatchVoiceOptions"
                                                       @change="handleDispatchVoiceCheckedChange"
                                                       style="margin-left: 10px">
                                        <el-checkbox v-for="item in DispatchVoiceOptions" :label="item.index"
                                                     :value="item.index"
                                                     class="elCheckboxMargin">{{item.name}}
                                        </el-checkbox>
                                    </el-checkbox-group>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                    <br/>
                    <div style="border:1px solid #EBEBEB ;padding: 10px " id="checkMsgAllExist">

                        <!------------------------消息------------------------->
                        <el-row>
                            <el-col :span="4" :style="menu.csscheckedMessagesOptionsAll">
                                <el-checkbox
                                        v-model="menu.checkedMessagesOptionsAll"
                                        :indeterminate="isIndeterminatecheckedMessagesOptions"
                                        @change="handlecsscheckedMessagesOptionsAllChange">消息
                                </el-checkbox>
                            </el-col>
                            <el-col :span="20">
                                <div style="border:1px solid #EBEBEB;border-top:none;border-right:none;border-bottom:none; ">
                                    <el-checkbox-group v-model="checkedMessagesOptions"
                                                       @change="handlecheckedMessagesOptionsChange"
                                                       style="margin-left: 10px">
                                        <el-checkbox v-for="item in MessagesOptions" :label="item.index"
                                                     :value="item.index"
                                                     class="elCheckboxMargin">{{item.name}}
                                        </el-checkbox>
                                    </el-checkbox-group>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                    <div style="border:1px solid #EBEBEB ;padding: 10px;visibility:unset" id="checkVideoAllExist">

                        <!------------------------对讲------------------------->
                        <el-row>
                            <el-col :span="4" :style="menu.csscheckedIntercomOptionsAll">

                                <el-checkbox


                                        v-model="menu.checkedIntercomOptionsAll"
                                        :indeterminate="isIndeterminatecheckedMessagesOptions"
                                        @change="handlecheckedIntercomOptionsAllChange">对讲
                                </el-checkbox>
                            </el-col>
                            <el-col :span="20">
                                <div style="border:1px solid #EBEBEB;border-top:none;border-right:none;border-bottom:none; ">
                                    <el-checkbox-group v-model="checkedIntercomOptions"
                                                       @change="handlecheckedIntercomOptionsChange"
                                                       style="margin-left: 10px">
                                        <el-checkbox v-for="item in IntercomOptions" :label="item.index"
                                                     :value="item.index"
                                                     class="elCheckboxMargin">{{item.name}}
                                        </el-checkbox>
                                    </el-checkbox-group>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                    <br/>
                    <!------------------------坐席------------------------->
                    <div style="border:1px solid #EBEBEB;padding: 10px ">
                        <el-row>
                            <el-col :span="4" :style="menu.csscheckedAgentOptionsAll">
                                <el-checkbox id="checkMapAllExist"
                                             v-model="menu.checkedAgentOptionsAll"
                                             :indeterminate="isIndeterminatecheckedAgentOptions"
                                             @change="handlecheckedAgentOptionsAllChange">坐席
                                </el-checkbox>
                            </el-col>
                            <el-col :span="20">
                                <div style="border:1px solid #EBEBEB;border-top:none;border-right:none;border-bottom:none; ">
                                    <el-checkbox-group v-model="checkedAgentOptions"
                                                       @change="handlecheckedAgentOptionsChange"
                                                       style="margin-left: 10px">
                                        <el-checkbox v-for="item in AgentOptions" :label="item.index"
                                                     :value="item.index"
                                                     class="elCheckboxMargin">{{item.name}}
                                        </el-checkbox>
                                    </el-checkbox-group>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                    <!------------------------设置------------------------->

                    <div style="border:1px solid #EBEBEB ;padding: 10px">
                        <el-row>
                            <el-col :span="4" :style="menu.csscheckedSystemOptionsmAll">
                                <el-checkbox id="checkSystemAllExist" @change="handleCheckedSystemOptionsAllChange"
                                             :indeterminate="isIndeterminatecheckedSystemOptions"
                                             v-model="menu.checkedSystemOptionsmAll">设置
                                </el-checkbox>
                            </el-col>
                            <el-col :span="20">
                                <div style="border:1px solid #EBEBEB;border-top:none;border-right:none;border-bottom:none; ">
                                    <el-checkbox-group v-model="checkedSystemOptions"
                                                       @change="handleCheckedSystemOptionsChange"
                                                       style="margin-left: 10px">
                                        <el-checkbox v-for="item in systemOptions" :label="item.index"
                                                     :value="item.index"
                                                     class="elCheckboxMargin">{{item.name}}
                                        </el-checkbox>
                                    </el-checkbox-group>
                                </div>
                            </el-col>


                        </el-row>
                    </div>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button  @click="showBox = false" class="dataManageBtn22BtnpPx">取 消</el-button>
                    <el-button  type="primary" @click="dialogComfirm" class="dataManageBtn22BtnpPx">确 定
                    </el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
  import AuthorityGroupManage from '@/api/dataManages/AuthorityGroupManage';
  import AuthorityGroupManageDatas from './AuthorityGroupManageDatas';
  import {mapActions} from 'vuex';

  export default {
    name: 'AuthorityGroupManage',
    props: {
      FleetId: Number,
      userId: Number,
    },
    data() {
      return {
        name: '',
        tableTitle: [
          {label: '权限组', prop: 'name', overflowTooltip: true},
          {label: '描述', prop: 'remark', overflowTooltip: true},
        ],
        rules: {
          name: [
            {required: true, message: '请输入权限组名称', trigger: 'blur'},
            {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur'},
          ], remark: [
            {required: false, message: '请输入描述', trigger: 'blur'},
            {min: 1, max: 128, message: '长度不能超过128个字符', trigger: 'blur'},
          ],
        },
        tableData: [],
        total: 0,
        type: 0,
        pageNum: 1,
        pageSize: 8,
        actualTotal: 10,
        buttons: [{label: '编辑', href: 'update'}, {label: '删除', href: 'delete'}],
        showBox: false,
        title: '',
        form: {
          id: '',
          name: '',
          remark: '',
          license: 0,
          Userlicense: 0,
        },
        checkedIndex: [],
        checkedMenuOptions: [],//一级菜单
        menuOptions: [],
        checkedDataOptions: [],

        menu: {
          options: [],
          //二级菜单 调度
          checkedDispatchVoiceOptionsAll: false,
          csscheckedDispatchVoiceOptionsAll: {
            'visibility': 'hidden',
          },
          //二级菜单 对讲
          checkedIntercomOptionsAll: false,
          csscheckedIntercomOptionsAll: {
            'visibility': 'hidden',
          },
          //二级菜单 消息
          checkedMessagesOptionsAll: false,
          csscheckedMessagesOptionsAll: {
            'visibility': 'hidden',
          },
          //二级菜单 坐席
          checkedAgentOptionsAll: false,
          csscheckedAgentOptionsAll: {
            'visibility': 'hidden',
          },
          //二级菜单 设置
          checkedSystemOptionsmAll: false,
          csscheckedSystemOptionsmAll: {
            'visibility': 'hidden',
          },
        },
        totalCheckOptions: [],//远端获取全部权限

        //二级菜单 调度
        checkedDispatchVoiceOptions: [],
        checkedDispatchVoiceOptionsALL: [],//调度全部二级菜单
        isIndeterminateDispatchVoice: false,
        isIndeterminatecheckedIntercomOptions: false,
        isIndeterminatecheckedMessagesOptions: false,
        isIndeterminatecheckedAgentOptions: false,
        isIndeterminatecheckedSystemOptions: false,

        //调度 二级菜单
        DispatchVoiceOptions: [],

        //二级菜单 对讲
        checkedIntercomOptions: [],
        //对讲 二级菜单
        IntercomOptions: [],
        checkedIntercomOptionsALL: [],//对讲全部二级菜单

        //二级菜单 消息
        checkedMessagesOptions: [],
        //消息 二级菜单
        MessagesOptions: [],
        checkedMessagesOptionsAll: [],

        //二级菜单 坐席
        checkedAgentOptions: [],
        //坐席 二级菜单
        AgentOptions: [],
        checkedAgentOptionsAll: [],

        //二级菜单 系统管理 - 系统设置
        checkedSystemOptions: [],
        //设置 二级菜单
        systemOptions: [],
        checkedSystemOptionsALL: [],//设置全部二级菜单
        showSwitch: [],
        AuthorityGroupManageDatas: [
          {
            id: 91,
            index: 0,
            name: '调度',
            switch: false,
          }, {
            id: 92,
            index: 1,
            name: '对讲',
            switch: false,
          }, {
            id: 93,
            index: 2,
            name: '消息', switch: false,
          }, {
            id: 94,
            index: 3,
            name: '坐席', switch: false,
          }, {
            id: 95,
            index: 4,
            name: '设置', switch: false,
          }, {
            id: 96,
            index: 5,
            name: '功能页', switch: false,
          }, {
            id: 97,
            index: 6,
            name: '个人主页', switch: false,
          }, {
            id: 98,
            index: 7,
            name: '系统通讯录', switch: false,
          }, {
            id: 99,
            index: 8,
            name: '历史通话', switch: false,
          }, {
            id: 100,
            index: 9,
            name: '通知查看', switch: false,
          }, {
            id: 101,
            index: 10,
            name: '会议', switch: false,
          }, {
            id: 102,
            index: 11,
            name: '广播通知', switch: false,
          }, {
            id: 103,
            index: 12,
            name: '组织架构管理', switch: false,
          }, {
            id: 104,
            index: 13,
            name: '调度台用户管理', switch: false,
          }, {
            id: 105,
            index: 14,
            name: '终端用户管理', switch: false,
          }, {
            id: 106,
            index: 15,
            name: '对讲组管理', switch: false,
          }, {
            id: 107,
            index: 16,
            name: '个人通讯录管理', switch: false,
          }, {
            id: 108,
            index: 17,
            name: '系统通讯录管理', switch: false,
          }, {
            id: 109,
            index: 18,
            name: '权限组管理', switch: false,
          }, {
            id: 110,
            index: 19,
            name: '角色管理', switch: false,
          }, {
            id: 111,
            index: 20,
            name: '紧急号码管理', switch: false,
          }, {
            id: 112,
            index: 21,
            name: '虚拟组管理', switch: false,
          },
          //新增权限：日志审计管理  坐席》强插、强拆、监听、强断、桥接
          {
            id: 113,
            index: 22,
            name: '日志审计管理', switch: false,
          },

          {
            id: 114,
            index: 23,
            name: '强插', switch: false,
          },
          {
            id: 115,
            index: 24,
            name: '强拆', switch: false,
          },
          {
            id: 116,
            index: 25,
            name: '监听', switch: false,
          },
          {
            id: 117,
            index: 26,
            name: '强断', switch: false,
          }, {
            id: 118,
            index: 27,
            name: '桥接', switch: false,
          }, {
            id: 119,
            index: 28,
            name: '代接', switch: false,
          },
        ],
      };
    },
    created() {
      this.changeDmgPage();
    },
    mounted() {
      this.query();
      this.queryLicense();
    },
    computed: {
      remarkA() {
        return this.form.remark;
      },
    },
    watch: {
      remarkA(val) {
        if (val.length > 100) {
          this.$message.error('描述不能超过100位字符');
          this.form.remark.substring(0, 100);
        }
      },
    },
    methods: {
      querySearch() {
        this.query(1);
        this.pageNum = 1;
      },
      changeDmgPage() {
        let w = window.screen.width;
        let h = window.screen.height;
        if (w == 1920) {
          if (h == 1080) {
          } else {
            this.pageSize = 11;
          }
        }
      },
      //表头样式
      headerCellClassName() {
        return 'font-size:18px;color:#515A6E;padding:10px 0px 10px 0px !important;background:#F5F5F6;border:1px solid #EBEBEB';
      },
      //表格样式
      cellStyleClassName() {
        return 'font-size:13px;text-align:center;color:#606266;padding:5px 0px 5px 0px !important;border:1px solid #EBEBEB';
      },
      handleCheckedChange: function(value) {
        var checkedCount = value.length;
      },
      closeDialog() {
        this.checkedDispatchVoiceOptions = [];
        this.checkedIntercomOptions = [];
        this.checkedMessagesOptions = [];
        this.checkedAgentOptions = [];
        this.checkedSystemOptions = [];
        this.$nextTick(function() {
          this.$refs['form'].resetFields();
        });
      },
      formClear: function() {
        this.form.id = '';
        this.form.name = '';
        this.form.remark = '';
        this.form.function = 0;
        this.form.data = 0;
        this.menu.options = [];
        this.checkedDispatchVoiceOptions = [];
        this.checkedIntercomOptions = [];
        this.checkedMessagesOptions = [];
        this.checkedAgentOptions = [];
        this.checkedSystemOptions = [];
        this.menu.checkedDispatchVoiceOptionsAll = false;
        this.menu.checkedIntercomOptionsAll = false;
        this.menu.checkedMessagesOptionsAll = false;
        this.menu.checkedAgentOptionsAll = false;
        this.menu.checkedSystemOptionsmAll = false;
        this.isIndeterminateDispatchVoice = false;
        this.isIndeterminatecheckedIntercomOptions = false;
        this.isIndeterminatecheckedMessagesOptions = false;
        this.isIndeterminatecheckedAgentOptions = false;
        this.isIndeterminatecheckedSystemOptions = false;

      },
      //vuex获取store中方法
      ...mapActions('user', ['chageGlobalUserRoles']),
      query: function(pageNum) {
        pageNum = pageNum || this.pageNum;

        this.queryLicense();
        var data = {
          name: this.name || '',
          fleetId: this.FleetId,
          pageNum: pageNum,//this.pageNum
          pageSize: this.pageSize,
        };
        AuthorityGroupManage.getAuthorityGroup(data).then(res => {
          var data = res;
          if (res.success) {
            this.tableData = data.obj.list;
            this.total = data.obj.total;
            this.chageGlobalUserRoles(this.$store.getters.userAccount);
          } else {
            this.$message('');
          }
          this.name = '';
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      //调度全选
      handlecheckedDispatchVoiceOptionsAllAllChange(val) {
        this.checkedDispatchVoiceOptions = val ? this.checkedDispatchVoiceOptionsALL : [];
        this.isIndeterminateDispatchVoice = false;
        console.log(this.checkedDispatchVoiceOptions);
      },
      //调度选择更新
      handleDispatchVoiceCheckedChange(value) {
        console.log(value);
        let checkedCount = value.length;
        this.menu.checkedDispatchVoiceOptionsAll = checkedCount === this.checkedDispatchVoiceOptionsALL.length;
        this.isIndeterminateDispatchVoice = checkedCount > 0 && checkedCount <
            this.checkedDispatchVoiceOptionsALL.length;
        console.log('调度选择更新' + this.menu.checkedDispatchVoiceOptionsAll);
        console.log(this.checkedDispatchVoiceOptionsALL);
      },
      //消息全选
      handlecsscheckedMessagesOptionsAllChange(val) {
        this.checkedMessagesOptions = val ? this.checkedMessagesOptionsAll : [];
        this.isIndeterminateDispatchVoice = false;
      },
      /*      isIndeterminateDispatchVoice:false,
        isIndeterminatecheckedIntercomOptions:false,
        isIndeterminatecheckedMessagesOptions:false,
        isIndeterminatecheckedAgentOptions:false,
        isIndeterminatecheckedSystemOptions:false,*/
      //消息选择更新
      handlecheckedMessagesOptionsChange(value) {
        console.log(value);
        let checkedCount = value.length;
        this.menu.checkedMessagesOptionsAll = checkedCount === this.checkedMessagesOptionsAll.length;
        this.isIndeterminatecheckedMessagesOptions = checkedCount > 0 && checkedCount <
            this.checkedMessagesOptionsAll.length;
      },
      //对讲全选
      handlecheckedIntercomOptionsAllChange(val) {
        this.checkedIntercomOptions = val ? this.checkedIntercomOptionsALL : [];
        this.isIndeterminatecheckedIntercomOptions = false;
      },
      //对讲选择更新
      handlecheckedIntercomOptionsChange(value) {
        console.log(value);
        let checkedCount = value.length;
        this.menu.checkedIntercomOptionsAll = checkedCount === this.checkedIntercomOptionsALL.length;
        this.isIndeterminatecheckedIntercomOptions = checkedCount > 0 && checkedCount <
            this.checkedIntercomOptionsALL.length;
      },

      //坐席全选
      handlecheckedAgentOptionsAllChange(val) {
        this.checkedAgentOptions = val ? this.checkedAgentOptionsAll : [];
        this.isIndeterminatecheckedAgentOptions = false;
      },
      //坐席选择更新
      handlecheckedAgentOptionsChange(value) {
        console.log(value);
        let checkedCount = value.length;
        this.menu.checkedAgentOptionsAll = checkedCount === this.checkedAgentOptionsAll.length;
        this.isIndeterminatecheckedAgentOptions = checkedCount > 0 && checkedCount < this.checkedAgentOptionsAll.length;
        console.log('坐席选择更新' + this.menu.checkedAgentOptionsAll);
        console.log(this.checkedAgentOptionsAll);
      },
      //设置全选
      handleCheckedSystemOptionsAllChange(val) {
        this.checkedSystemOptions = val ? this.checkedSystemOptionsALL : [];
        this.isIndeterminatecheckedSystemOptions = false;
        console.log('全选绑定' + this.menu.checkedSystemOptionsmAll);
        console.log('设置更新' + this.isIndeterminatecheckedSystemOptions);
        console.log(val);
      },
      //设置更新
      handleCheckedSystemOptionsChange(value) {
        console.log(value);
        let checkedCount = value.length;
        this.menu.checkedSystemOptionsALL = checkedCount === this.checkedSystemOptionsALL.length;
        this.isIndeterminatecheckedSystemOptions = checkedCount > 0 && checkedCount <
            this.checkedSystemOptionsALL.length;
        console.log(checkedCount);
        console.log('设置更新checkedSystemOptionsALL：' + this.menu.checkedSystemOptionsALL);
      },
      //对checkbox进行分组
      funCheckGroup() {
        //系统设置
        this.DispatchVoiceOptions = [];
        this.IntercomOptions = [];
        this.MessagesOptions = [];
        this.AgentOptions = [];
        this.systemOptions = [];
        this.checkedDispatchVoiceOptionsALL = [];
        this.checkedSystemOptionsALL = [];
        this.checkedAgentOptionsAll = [];
        // index  0-5为 一级菜单
        for (var i = 0; i < this.totalCheckOptions.length; i++) {
          if (91 <= this.totalCheckOptions[i].id && this.totalCheckOptions[i].id <= 95) {
            //主菜单的ID,用于前面做全选
            this.menu.options.push(this.totalCheckOptions[i]);
            if (this.totalCheckOptions[i].id === 91) {
              this.menu.csscheckedDispatchVoiceOptionsAll.visibility = 'none';
              // this.menu.checkedDispatchVoiceOptionsAll = true;

            } else if (this.totalCheckOptions[i].id === 92) {
              /*  $('.checkMapAllExist').css({
                  visibility: "visible"
                });*/
              this.menu.csscheckedIntercomOptionsAll.visibility = 'none';
              // this.menu.checkedIntercomOptionsAll = false;

            } else if (this.totalCheckOptions[i].id === 93) {
              this.menu.csscheckedMessagesOptionsAll.visibility = 'none';
              // this.menu.checkedMessagesOptionsAll = true;

            } else if (this.totalCheckOptions[i].id === 94) {
              this.menu.csscheckedAgentOptionsAll.visibility = 'none';
              // this.menu.checkedAgentOptionsAll = true;

            } else if (this.totalCheckOptions[i].id === 95) {
              this.menu.csscheckedSystemOptionsmAll.visibility = 'none';
              // this.menu.checkedSystemOptionsmAll = true;
            }
          } else if ([96, 97, 98, 99, 100, 101, 102].indexOf(this.totalCheckOptions[i].id) > -1) {
            this.DispatchVoiceOptions.push(this.totalCheckOptions[i]);//调度 二级菜单
            // this.checkedDispatchVoiceOptionsALL.push(this.totalCheckOptions[i].id);//调度 二级菜单
            this.checkedDispatchVoiceOptionsALL.push(this.totalCheckOptions[i].index);//调度 二级菜单
          } else if ([114, 115, 116, 117, 118, 119].indexOf(this.totalCheckOptions[i].id) > -1) {
            this.AgentOptions.push(this.totalCheckOptions[i]);//坐席 二级菜单
            // this.checkedDispatchVoiceOptionsALL.push(this.totalCheckOptions[i].id);//坐席 二级菜单
            this.checkedAgentOptionsAll.push(this.totalCheckOptions[i].index);//坐席 二级菜单
          } else if ([103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113].indexOf(this.totalCheckOptions[i].id) >
              -1) {
            this.systemOptions.push(this.totalCheckOptions[i]);////设置 二级菜单
            // this.checkedSystemOptionsALL.push(this.totalCheckOptions[i].id);//调度 二级菜单
            this.checkedSystemOptionsALL.push(this.totalCheckOptions[i].index);//调度 二级菜单
          }
        }

      },
      funCheckedGroup(checkedLisense) {
        this.checkedDispatchVoiceOptions = [];
        this.checkedIntercomOptions = [];
        this.checkedMessagesOptions = [];
        this.checkedAgentOptions = [];
        this.checkedSystemOptions = [];
        // index  0-5为 一级菜单 组array回填数据 a.indexOf(b)
        //是在字符串变bai量dua中查找字符串变量b首次出现的位zhi置,如果没有找到就dao会返回-1
        for (let i = 0; i < checkedLisense.length; i++) {
          if (91 <= checkedLisense[i].id && checkedLisense[i].id <= 95) {
            //主菜单的ID,用于前面做全选
            if (checkedLisense[i].id === 91) {
              this.menu.csscheckedDispatchVoiceOptionsAll.visibility = 'none';
              // this.menu.checkedDispatchVoiceOptionsAll = true;
              // this.isIndeterminateDispatchVoice = false;

            } else if (checkedLisense[i].id === 92) {
              this.menu.csscheckedIntercomOptionsAll.visibility = 'none';
              this.menu.checkedIntercomOptionsAll = true;
              // this.isIndeterminatecheckedIntercomOptions = true;

            } else if (checkedLisense[i].id === 93) {
              this.menu.csscheckedMessagesOptionsAll.visibility = 'none';
              this.menu.checkedMessagesOptionsAll = true;
              // this.isIndeterminatecheckedMessagesOptions = true;

            } else if (checkedLisense[i].id === 94) {
              this.menu.csscheckedAgentOptionsAll.visibility = 'none';
              this.menu.checkedAgentOptionsAll = true;
              // this.isIndeterminatecheckedAgentOptions = true;

            } else if (checkedLisense[i].id === 95) {
              this.menu.csscheckedSystemOptionsmAll.visibility = 'none';
              this.menu.checkedSystemOptionsmAll = true;
              // this.isIndeterminatecheckedSystemOptions = false;
            }
          } else if ([96, 97, 98, 99, 100, 101, 102].indexOf(checkedLisense[i].id) > -1) {
            // this.checkedDispatchVoiceOptions.push(checkedLisense[i].id);
            this.checkedDispatchVoiceOptions.push(checkedLisense[i].index);

          } else if ([114, 115, 116, 117, 118, 119].indexOf(checkedLisense[i].id) > -1) {
            this.checkedAgentOptions.push(checkedLisense[i].index);

          } else if ([103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113].indexOf(checkedLisense[i].id) > -1) {
            this.checkedSystemOptions.push(checkedLisense[i].index);
          }
        }
        let checkedCount = this.checkedDispatchVoiceOptions.length;
        this.menu.checkedDispatchVoiceOptionsAll = checkedCount === this.checkedDispatchVoiceOptionsALL.length;
        this.isIndeterminateDispatchVoice = checkedCount > 0 && checkedCount <
            this.checkedDispatchVoiceOptionsALL.length;
        let checkedCount2 = this.checkedSystemOptions.length;
        this.menu.checkedSystemOptionsmAll = checkedCount2 === this.checkedSystemOptionsALL.length;
        this.isIndeterminatecheckedSystemOptions = checkedCount2 > 0 && checkedCount2 <
            this.checkedSystemOptionsALL.length;

        this.showBox = true;
        this.$nextTick(function() {
          $('.el-checkbox__inner').css('display', 'block');
        });

      },
      queryLicense() {
        //TODO 本地设置权限组
        for (let i = 0; i < this.AuthorityGroupManageDatas.length; i++) {
          this.AuthorityGroupManageDatas[i]['index'] = i;
        }
        this.totalCheckOptions = this.AuthorityGroupManageDatas;

        this.actualTotal = this.totalCheckOptions.length;
        this.funCheckGroup();
        var data = {
          // fleetId: this.FleetId,
          versionType: 1,
        };
        /*    AuthorityGroupManage.queryAirportLicense(data).then(res => {
              if (res.success) {

                // this.actualTotal = data.obj.actualTotal;
                for (let i = 0; i < res.obj.list.length; i++) {
                  res.obj.list[i]['index'] = i;
                }
                this.totalCheckOptions = res.obj.list;
                this.actualTotal = this.totalCheckOptions.length;
                this.funCheckGroup();
              } else {
                for (let i = 0; i < AuthorityGroupManageDatas.length; i++) {
                  AuthorityGroupManageDatas[i]['index'] = i;
                }
                this.totalCheckOptions = AuthorityGroupManageDatas;

                this.actualTotal = this.totalCheckOptions.length;
                this.funCheckGroup();
              }
            }).catch(err => {
              // 异常情况
              console.log(err);
            });*/
      },
      getAuthorityOptions: function() {

        AuthorityGroupManage.getAuthorityOptions({}).then(res => {
          var data = res;
          if (res.success) {
            this.authorityOptions = data.obj.list;
          } else {
            this.$message(res.msg);
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      add() {
        this.queryLicense();
        this.formClear();
        this.funCheckGroup();//默认勾选
        console.log('this.checkedDispatchaddaddaddadd');
        console.log(this.checkedDispatchVoiceOptions);
        this.type = 1;
        this.title = '添加权限组';
        this.showBox = true;
        /*   this.$nextTick(function () {
             $('.el-checkbox__inner').css('display', 'block');
           })*/
      },
      funGetMenuLisence() {
        console.log('this.menu.options');
        console.log(this.menu.options);

        var menuLicense = [];
        //调度菜单
        console.log('调度菜单' + this.menu.checkedDispatchVoiceOptionsAll);
        if (this.menu.checkedDispatchVoiceOptionsAll === true) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 91) {
              menuLicense.push(this.menu.options[i].index);
              break;
            }
          }
        }
        ;
        if (this.checkedDispatchVoiceOptions.length > 0) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 91) {
              menuLicense.push(this.menu.options[i].index);
              break;
            }
          }
        }

        //对讲菜单
        console.log('对讲菜单' + this.menu.checkedIntercomOptionsAll);
        if (this.menu.checkedIntercomOptionsAll === true) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 92) {
              menuLicense.push(this.menu.options[i].index);
              break;

            }
          }
        }
        ;
        if (this.checkedIntercomOptions.length > 0) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 92) {
              menuLicense.push(this.menu.options[i].index);
              break;
            }
          }
        }

        //消息菜单
        console.log('消息菜单' + this.menu.checkedMessagesOptionsAll);
        if (this.menu.checkedMessagesOptionsAll === true) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 93) {
              menuLicense.push(this.menu.options[i].index);
              break;

            }
          }
        }
        ;
        if (this.checkedMessagesOptions.length > 0) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 93) {
              menuLicense.push(this.menu.options[i].index);
              break;
            }
          }
        }
        //坐席菜单
        console.log('坐席菜单' + this.menu.checkedAgentOptionsAll);
        if (this.menu.checkedAgentOptionsAll === true) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 94) {
              menuLicense.push(this.menu.options[i].index);
              break;

            }
          }
        }
        ;
        if (this.checkedAgentOptions.length > 0) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 94) {
              menuLicense.push(this.menu.options[i].index);
              break;
            }
          }
        }
        //设置菜单
        console.log('设置菜单' + this.menu.checkedSystemOptionsmAll);
        if (this.menu.checkedSystemOptionsmAll === true) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 95) {
              menuLicense.push(this.menu.options[i].index);
              break;

            }
          }
        }
        ;
        if (this.checkedSystemOptions.length > 0) {
          for (var i = 0; i < this.menu.options.length; i++) {
            if (this.menu.options[i].id === 95) {
              menuLicense.push(this.menu.options[i].index);
              break;
            }
          }
        }
        /* console.log('menuLicensemenuLicensemenuLicensemenuLicense');
         console.log(Array.from(new Set(menuLicense)));*/

        return Array.from(new Set(menuLicense));
        // return menuLicense;
        // return [0,0,0,0];
      },
      dialogComfirm: function() {
        console.log('this.checkedDispatchVoiceOptions00000000000000');
        console.log(this.checkedDispatchVoiceOptions);
        console.log('this.checkedDispatchVoiceOptions00000000000000');
        console.log(this.checkedIntercomOptions);
        console.log(this.checkedMessagesOptions);
        console.log(this.checkedAgentOptions);
        console.log(this.checkedSystemOptions);
        var validate = false;
        this.$refs['form'].validate((valid) => {
          if (valid) {
            validate = true;
          } else {
            console.log('error submit!!');
            validate = false;
            return false;
          }
        });
        if (validate === false) {
          return;
        }
        var that = this;
        var license = '';
        for (var i = 0; i < that.actualTotal; i++) {
          license = '0' + license;
        }
        var menuLicense = this.funGetMenuLisence();
        // var menuLicense = [91]
        for (var i = 0; i < menuLicense.length; i++) {
          license = license.substr(0, menuLicense[i]) + '1' + license.substr(menuLicense[i] + 1);

        }
        console.log('菜单license');
        console.log(license);
        for (var i = 0; i < that.checkedDispatchVoiceOptions.length; i++) {
          license = license.substr(0, that.checkedDispatchVoiceOptions[i]) + '1' +
              license.substr(that.checkedDispatchVoiceOptions[i] + 1);
        }
        for (var i = 0; i < that.checkedIntercomOptions.length; i++) {
          license = license.substr(0, that.checkedIntercomOptions[i]) + '1' +
              license.substr(that.checkedIntercomOptions[i] + 1);

        }
        for (var i = 0; i < that.checkedMessagesOptions.length; i++) {
          license = license.substr(0, that.checkedMessagesOptions[i]) + '1' +
              license.substr(that.checkedMessagesOptions[i] + 1);

        }
        for (var i = 0; i < that.checkedAgentOptions.length; i++) {
          license = license.substr(0, that.checkedAgentOptions[i]) + '1' +
              license.substr(that.checkedAgentOptions[i] + 1);

        }
        for (var i = 0; i < that.checkedSystemOptions.length; i++) {
          license = license.substr(0, that.checkedSystemOptions[i]) + '1' +
              license.substr(that.checkedSystemOptions[i] + 1);

        }
        /*       var menuLicense = this.funGetMenuLisence();
               for (var i = 0; i < menuLicense.length; i++) {
                 license = license.substr(0, menuLicense[i]) + '1' + license.substr(menuLicense[i] + 1);

               }*/

        //转化为十进制传到后台
        var licenseNum = license;
        // var licenseNum = parseInt(license, 2);
        console.log(licenseNum);

        //0 -编辑 1-添加
        if (that.type === 0) {
          var data = {
            id: that.form.id,
            name: that.form.name,
            remark: that.form.remark,
            license: licenseNum,
          };

          var auditData = {
            // dcId: that.$store.getters.account,
            dcId: this.$store.getters.userAccount,
            type: 5,
            status: '成功',
            describe: that.$store.getters.userName + '修改了权限组:' + data.name,
            time: new Date,
            fleetId: that.$store.getters.scsFleetId,
            detailType: 29,
            remark: '',
          };
          AuthorityGroupManage.updateAuthorityGroup(data).then(res => {
            if (res.success) {
              this.GlobalFunc.GlaAddAudit(auditData);
              that.closeBox();
              that.$message.success('编辑成功');
              that.query();

            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);

              that.$message(res.msg);
            }
          }).catch(err => {
            // 异常情况
            console.log(err);
            auditData.status = '异常';
            this.GlobalFunc.GlaAddAudit(auditData);
          });
        } else {

          var data = {
            name: that.form.name,
            remark: that.form.remark,
            fleetId: this.FleetId,
            license: licenseNum,
          };
          var auditData = {
            dcId: that.$store.getters.userAccount,
            type: 5,
            status: '成功',
            describe: that.$store.getters.userName + '添加权限组:' + data.name,
            time: new Date,
            fleetId: that.$store.getters.scsFleetId,
            detailType: 29,
            remark: '',
          };
          console.info('%c auditData=' + auditData, 'color:red');

          AuthorityGroupManage.addAuthorityGroup(data).then(res => {
            if (res.success) {

              this.GlobalFunc.GlaAddAudit(auditData);
              that.closeBox();
              that.$message.success('添加成功');
              that.query();

            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);

              that.$message.error('添加失败');
            }
          }).catch(err => {
            // 异常情况
            console.log(err);
            auditData.status = '异常';
            this.GlobalFunc.GlaAddAudit(auditData);
          });
        }

      },
      addAuthorityGroup: function() {
        var that = this;
        for (var i = 0; i < that.checkedFunctionOptions.length; i++) {
          this.form.function += that.checkedFunctionOptions[i];
        }
        for (var i = 0; i < that.checkedDataOptions.length; i++) {
          this.form.data += that.checkedDataOptions[i];
        }
        var data = {
          name: that.form.name,
          remark: that.form.remark,
          function: that.form.function,
          data: that.form.data,
        };
        var auditData = {
          dcId: this.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: that.$store.getters.userName + '添加了权限组:' + data.name,
          time: new Date,
          fleetId: that.$store.getters.scsFleetId,
          detailType: 29,
          remark: '',
        };

        console.info('%c auditData=' + auditData, 'color:red');
        AuthorityGroupManage.addAuthorityGroup(data).then(res => {
          if (res.success) {
            if (res.obj.count === 1) {
              this.GlobalFunc.GlaAddAudit(auditData);

              that.closeBox();
              that.$message.success('添加成功');
              that.query();
            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);

              that.$message('错误码' + res.obj.count);
            }
          } else {
            auditData.status = '失败';
            this.GlobalFunc.GlaAddAudit(auditData);
            that.$message('内部错误');
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
          auditData.status = '异常';
          this.GlobalFunc.GlaAddAudit(auditData);
        });
      },
      updateAuthorityGroup: function() {
        var that = this;
        for (var i = 0; i < that.checkedFunctionOptions.length; i++) {
          this.form.function += that.checkedFunctionOptions[i];
        }
        for (var i = 0; i < that.checkedDataOptions.length; i++) {
          this.form.data += that.checkedDataOptions[i];
        }
        var data = {
          id: that.form.id,
          name: that.form.name,
          remark: that.form.remark,
          function: that.form.function,
          data: that.form.data,
        };
        var auditData = {
          dcId: this.$store.getters.userAccount,
          type: 5,
          status: '成功',
          describe: that.$store.getters.userName + '修改了权限组:' + data.name,
          time: new Date,
          fleetId: that.$store.getters.scsFleetId,
          detailType: 29,
          remark: '',
        };

        AuthorityGroupManage.updateAuthorityGroup(data).then(res => {
          if (res.success) {
            if (res.obj.count === 1) {
              this.GlobalFunc.GlaAddAudit(auditData);
              that.closeBox();
              that.$message.success('编辑成功');
              that.query();
            } else {
              auditData.status = '失败';
              this.GlobalFunc.GlaAddAudit(auditData);
              that.$message('错误码' + res.obj.count);
            }
          } else {
            auditData.status = '失败';
            this.GlobalFunc.GlaAddAudit(auditData);
            that.$message('内部错误');
          }
        }).catch(err => {
          // 异常情况
          console.log(err);
          auditData.status = '异常';
          this.GlobalFunc.GlaAddAudit(auditData);
        });
      },
      closeBox: function() {
        this.formClear();
        this.showBox = false;
      },
      //License解码
      decode(license, daslength) {
        if (typeof license === 'undefined') {
          license = 0;
        }
        // let license_decoded = ('000000' + license.toString(2)).split('').reverse();
        let license_decoded = license.toString(2);
        console.log(license_decoded);

        function buling(a, length) {
          return a.padStart(length, 0);
        }

        let getlicense_decoded = buling(license_decoded, daslength); //'00010010'

        getlicense_decoded.split('');
        var temp = [];
        let returnRules = (() => {
          var temp = [];
          getlicense_decoded.split('').forEach(function(value, index) {
            temp[index] = value === '1';
          });
          return temp;
        })();
        console.log(returnRules);
        return returnRules;
      },
      getSelfLise(arrA, arrB) {
        for (let a = 0; a < arrB.length; a++) {
          console.log(arrB[a].switch);
          if (arrB[a].switch) {
            arrB[a].switch = false;
          }
        }
        console.log('arrB');
        console.log(arrB);
        for (let j = 0; j < arrB.length; j++) {
          for (let i = 0; i < arrA.length; i++) {
            if (arrA[i]) {
              arrB[i].switch = true;
            }
          }
        }
        let fitterDatas = [];
        for (let c = 0; c < arrB.length; c++) {
          if (arrB[c].switch) {
            fitterDatas.push(arrB[c]);
          } else {
          }
        }
        return fitterDatas;
      },
      editLicenseForUser(userlicense) {

        console.log(userlicense);
        let Authority = new Array();
        for (let i = 0; i < this.AuthorityGroupManageDatas.length; i++) {
          Authority.push(this.AuthorityGroupManageDatas[i]);
        }
        console.log('this.AuthorityGroupManageDatas');
        console.log(Authority);
        let AuthorityDatasLen = Authority.length;
        let decodeDatas = this.decode(userlicense, AuthorityDatasLen);
        let returnData = this.getSelfLise(decodeDatas, Authority);
        console.log('returnData');
        console.log(returnData);
        this.funCheckedGroup(returnData);
      },
      queryGroupLicense(id) {
        AuthorityGroupManage.queryPossessLicense(
            {
              id: id,
              fleetId: this.FleetId,
              versionType: 1,

            },
        ).then(res => {
          if (res.success) {
            var license = res.obj;
            this.funCheckedGroup(license);

          }
        }).catch(err => {
          // 异常情况
          console.log(err);
        });
      },
      operate: function(row, href) {
        this.formClear();
        if (href.indexOf('update') !== -1) {
          this.type = 0;
          this.title = '编辑';
          this.form.id = row.id;
          this.form.name = row.name;
          this.form.remark = row.remark;
          this.form.Userlicense = row.license;
          this.queryLicense();
          // this.queryGroupLicense(row.id);
          this.editLicenseForUser(row.license);//编辑通过权限回显表单
          this.showBox = true;

        } else if (href.indexOf('delete') !== -1) {

          var auditData = {
            dcId: this.$store.getters.userAccount,
            type: 5,
            status: '成功',
            describe: this.$store.getters.userName + '删除权限组:' + row.name,
            time: new Date,
            fleetId: this.$store.getters.scsFleetId,
            detailType: 29,
            remark: '',
          };

          this.$confirm('是否确定删除', '', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            AuthorityGroupManage.deleteAuthorityGroup({id: row.id}).then(res => {
              if (res.success) {
                this.GlobalFunc.GlaAddAudit(auditData);
                this.$message.success('删除成功');
                //此时已经实现删除操作，所以total的值需要减1，Math.ceil是向上取整，保证始终大于等于1
                const totalPage = Math.ceil((this.total - 1) / this.pageSize);
                //记住删除最后一条数据时当前码是最后一页
                const tempPageNum = this.pageNum > totalPage ? totalPage : this.pageNum;
                //实现跳转
                this.pageNum = tempPageNum < 1 ? 1 : tempPageNum;
                this.query();
              } else {
                if (res.errorNo == 1) {
                  this.$message.error('该权限组包含角色,无法删除');
                } else {
                  auditData.status = '失败';
                  this.GlobalFunc.GlaAddAudit(auditData);
                }

              }
            }).catch(err => {
              // 异常情况
              console.log(err);
              auditData.status = '异常';
              this.GlobalFunc.GlaAddAudit(auditData);
            });
          }).catch(() => {
          });

        } else {
        }
      },
      changePageNum: function(val) {
        this.pageNum = val;
        this.query();
      },
    },
  };
</script>

<style scoped>
    .el-dialog__body {
        /*height: 30vh;*/
        overflow: auto;
    }

    /deep/ .el-form-item__label {
        font-size: 22px!important;
    }

    /deep/ .el-input__inner {
        font-size: 22px!important;
    }

    /deep/ .el-checkbox__label {
        font-size: 20px;
    }

    /deep/ .el-dialog__title {
        font-size: 22px;
    }

    /deep/ .el-checkbox {
        height: 30px;
        width: 30px;
    }

    /deep/ .el-checkbox__inner {
        width: 30px;
        height: 30px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner {
        height: 30px;
        width: 30px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__input {
        vertical-align: middle!important;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner:before {
        font-size: 20px;
        margin: 3px auto auto 3px;
        text-align: center;
        line-height: 40px;
    }

    /deep/ .el-checkbox /deep/ .el-checkbox__inner:after {
        height: 12px;
        width: 12px;
        font-size: 20px;
        margin: 3px auto auto 3px;
        text-align: center;
        /*line-height: 40px;*/
    }
    /deep/ .el-tree-node__content {
        height: 40px !important;
    }

    .elCheckboxMargin {
        width: 180px;
        margin: 10px
    }

    /*分页修改开始*/
    .el-pagination /deep/ .el-pagination__total {
        font-size: 20px;
        line-height: 60px;
    }

    .el-pagination /deep/ button /deep/ .el-icon .el-icon .el-icon-arrow-left {
        font-size: 50px;
        height: 60px;
        width: 40px;
    }

    /deep/ .el-pager li {
        margin-top: 15px;
        height: 60px;
        width: 60px;
        line-height: 60px;
        font-size: 18px;
    }

    /deep/ .el-pagination.is-background .btn-prev, .el-pager li {
        height: 60px;
        width: 60px;
    }

    /deep/ .el-pagination.is-background .btn-next, .el-pager li {
        height: 60px;
        width: 60px;
    }

    /deep/ .el-pagination .btn-prev .el-icon {
        font-size: 40px;
    }

    /deep/ .el-pagination .btn-next .el-icon {
        font-size: 40px;
    }

    /*分页修改结束*/
    .searchFormBtn {
        margin-top: 1%;
        margin-left: 1%;
        font-size: 16px
    }

    .top10 {
        width: 10%
    }

    .table-wrap {
    }

    .tableHandle {
        width: 100%;
        text-align: center;
        font-size: 16px
    }

    .align-right {
        width: 230px;
    }

    .searchForm {
        margin-left: 5px;
    }
</style>
